package com.blockmeta.bbs.businesslibrary.Jsbridge;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.umeng.ShareRouter;
import com.blockmeta.bbs.businesslibrary.umeng.ShareType;
import com.blockmeta.bbs.businesslibrary.umeng.TypeDescribe;

import org.greenrobot.eventbus.EventBus;

public class MyJsBridge {
    private Context mActivity;

    private ShareRouter mShareRouter;

    public MyJsBridge(Context activity) {
        mActivity = activity;
    }

    @SuppressLint("JavascriptInterface")
    @JavascriptInterface
    public void test(String str) {
        if (!TextUtils.isEmpty(str)) {
            Toast.makeText(mActivity, str, Toast.LENGTH_SHORT).show();
        }
    }

    @SuppressLint("JavascriptInterface")
    @JavascriptInterface
    public void openDialog(String str) {
        if ("openlogin".equals(str)) {
            RouterHelper.startActivity(mActivity, RouterPath.PHONE_LOGIN_ACTIVITY);
        }
    }

    @SuppressLint("JavascriptInterface")
    @JavascriptInterface
    public void openShare(String title, String link, String desc, String imgUrl) {
        if (mShareRouter == null){
            String text = desc.substring(0, desc.length() > 140 ? 140 : desc.length());
            mShareRouter = new ShareRouter.Builder()
                    .context(mActivity)
                    .shareType(ShareType.NORMAL_WEB)
                    .typeDescribe(TypeDescribe.CHAIN_CRAFT)
                    .url(link)
                    .thumb(imgUrl)
                    .title(title)
                    .describe(text)
                    .build();
        }
        mShareRouter.show();
    }

    @SuppressLint("JavascriptInterface")
    @JavascriptInterface
    public void returnHome() {
        EventBus.getDefault().post(new Event_BackToHome());
    }

    @SuppressLint("JavascriptInterface")
    @JavascriptInterface
    public void customerService(String uid, String name) {
        LogUtils.i("chaininterface " + uid + " " + name);
        if(!TextUtils.isEmpty(uid)){
            RouterHelper.startActivity(mActivity, RouterPath.IM_UUChatActivity_ACTIVITY, uid + "");
        }
    }

    public static class Event_BackToHome {

    }

}
